package com.dzu.gulimall.cart.vo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author ZhaoDong
 * @date 2022/7/2 13:21
 * @description 整个购物车
 *
 * 需要计算的属性，需要重写他的get方法，保证每次获取属性都会进行计算
 */
public class Cart {

    private List<CartItem> items;

    /**
     * 商品数量
     */
    private Integer countNum;

    /**
     * 商品类型数量
     */
    private Integer countType;

    /**
     * 商品总价
     */
    private BigDecimal totalAmount;

    /**
     * 减免价格
     */
    private BigDecimal reduce = BigDecimal.ZERO;


    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public Integer getCountNum() {
        int count = 0;
        if (CollUtil.isNotEmpty(items)) {
            count = items.stream().map(CartItem::getCount).reduce(0, Integer::sum);
        }
        countNum = count;
        return countNum;
    }

    public Integer getCountType() {
        int count = 0;
        if (CollUtil.isNotEmpty(items)) {
            count = items.size();
        }
        countType = count;
        return countType;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal total = BigDecimal.ZERO;
        // 计算购物项总价
        if (CollUtil.isNotEmpty(items)) {
            total = items.stream().filter(CartItem::getCheck).map(CartItem::getTotalPrice).reduce(BigDecimal.ZERO, NumberUtil::add);
        }

        // 2、减去优惠总价
        totalAmount = NumberUtil.sub(total, reduce);

        return totalAmount;
    }


    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
